Print longer paths to Cargo.toml on failure
authorAlex Crichton <alex@alexcrichton.com>
Thu, 28 Aug 2014 19:28:23 +0000 (12:28 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 2 Sep 2014 18:45:25 +0000 (11:45 -0700)
Whenever possible, try to print a short path by using path_relative_from.

Closes #404

src/cargo/util/toml.rs
tests/test_cargo_compile.rs

index 117b68d629c46d289782fea3272f933c6ff21f06..a268e6b70bebc14f165931942ce8e03d9b1fde8a 100644 (file)
@@ -1,6 +1,7 @@
 use std::collections::HashMap;
 use std::fmt;
 use std::io::fs;
+use std::os;
 use std::slice;
 use std::str;
 use toml;
@@ -89,19 +90,26 @@ pub fn to_manifest(contents: &[u8],
                    source_id: &SourceId,
                    layout: Layout)
                    -> CargoResult<(Manifest, Vec<Path>)> {
+    let manifest = layout.root.join("Cargo.toml");
+    let manifest = match manifest.path_relative_from(&os::getcwd()) {
+        Some(path) => path,
+        None => manifest,
+    };
     let contents = try!(str::from_utf8(contents).require(|| {
-        human("Cargo.toml is not valid UTF-8")
+        human(format!("{} is not valid UTF-8", manifest.display()))
     }));
-    let root = try!(parse(contents, &Path::new("Cargo.toml")));
+    let root = try!(parse(contents, &manifest));
     let mut d = toml::Decoder::new(toml::Table(root));
     let toml_manifest: TomlManifest = match Decodable::decode(&mut d) {
         Ok(t) => t,
-        Err(e) => return Err(human(format!("Cargo.toml is not a valid \
-                                            manifest\n\n{}", e)))
+        Err(e) => return Err(human(format!("{} is not a valid \
+                                            manifest\n\n{}",
+                                           manifest.display(), e)))
     };
 
     let pair = try!(toml_manifest.to_manifest(source_id, &layout).map_err(|err| {
-        human(format!("Cargo.toml is not a valid manifest\n\n{}", err))
+        human(format!("{} is not a valid manifest\n\n{}",
+                      manifest.display(), err))
     }));
     let (mut manifest, paths) = pair;
     match d.toml {
@@ -146,7 +154,7 @@ pub fn parse(toml: &str, file: &Path) -> CargoResult<toml::Table> {
         let (loline, locol) = parser.to_linecol(error.lo);
         let (hiline, hicol) = parser.to_linecol(error.hi);
         error_str.push_str(format!("{}:{}:{}{} {}\n",
-                                   file.filename_display(),
+                                   file.display(),
                                    loline + 1, locol + 1,
                                    if loline != hiline || locol != hicol {
                                        format!("-{}:{}", hiline + 1,
index fc4d1250abfc4ad6580437dd1a6f6a0290cd25f0..eae7180f8f24fa0a68bc78240ac3b556a43f738d 100644 (file)
@@ -48,7 +48,6 @@ test!(cargo_compile_with_invalid_manifest {
                       No `package` or `project` section found.\n"))
 })
 
-
 test!(cargo_compile_with_invalid_manifest2 {
     let p = project("foo")
         .file("Cargo.toml", r"
@@ -63,6 +62,24 @@ test!(cargo_compile_with_invalid_manifest2 {
                       Cargo.toml:3:19-3:20 expected a value\n\n"))
 })
 
+test!(cargo_compile_with_invalid_manifest3 {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/Cargo.toml", "a = bar");
+
+    assert_that(p.cargo_process("build").arg("--manifest-path")
+                 .arg("src/Cargo.toml"),
+        execs()
+        .with_status(101)
+        .with_stderr("could not parse input TOML\n\
+                      src[..]Cargo.toml:1:5-1:6 expected a value\n\n"))
+})
+
 test!(cargo_compile_with_invalid_version {
     let p = project("foo")
         .file("Cargo.toml", r#"